import os
import sqlite3
from typing import Dict, Any
from tensorflow.python.keras import models
from TrainParam import TrainParam


class TrainTask(object):
    def __init__(self,
                 progress_save_path="./progress"):
        self.progress_save_path = progress_save_path

    @staticmethod
    def mkdir(path: str) -> None:
        if os.path.exists(path) is not True:
            os.makedirs(path)

    @staticmethod
    def insert_database(db_conn: Any, table: str, data: Dict[str, str]) -> None:
        keys = ', '.join(data.keys())
        holder = ', '.join(['?'] * len(data.keys()))
        command = 'insert into {} ({}) values ({})'.format(table, keys, holder)
        values = list(data.values())
        try:
            db_conn.execute(command, values)
        except Exception as error:
            print(error)
        db_conn.commit()

    def training(self, model: models.Model, x: Any, y: Any, param: TrainParam):
        model.fit(x=x,
                  y=y,
                  batch_size=param.batch_size,
                  epochs=param.epochs,
                  verbose=param.verbose,
                  callbacks=param.callbacks,
                  validation_split=param.validation_split,
                  validation_data=param.validation_data,
                  shuffle=param.shuffle,
                  class_weight=param.class_weight,
                  sample_weight=param.sample_weight,
                  initial_epoch=param.initial_epoch,
                  steps_per_epoch=param.steps_per_epoch,
                  validation_steps=param.validation_steps,
                  max_queue_size=param.max_queue_size,
                  workers=param.workers,
                  use_multiprocessing=param.use_multiprocessing)


if __name__ == '__main__':
    pass
